<template>
  <div class="container">
    <el-main>
      <!-- Repair Records -->
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        highlight-current-row
      >
        <el-table-column align="center" label="序号" wmaintain-idth="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="故障时间" width="160">
          <template slot-scope="scope">
            <span>{{ formatDate(scope.row.maintainTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="故障描述" align="center">
          <template slot-scope="scope">
            {{ scope.row.troubleDescribe }}
          </template>
        </el-table-column>
        <el-table-column
          class-name="maintainState-col"
          label="处理状态"
          wmaintain-idth="110"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.maintainState | maintainStateFilter">{{
              scope.row.maintainState
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="created_at"
          label="操作"
          wmaintain-idth="250"
        >
          <template slot-scope="scope">
            <!-- <el-button
                type="primary"
                round
                size="mini"
                @click="viewRecord(scope.row)"
              >查看</el-button> -->
            <el-button
              type="primary"
              round
              size="mini"
              @click="editRecord(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              round
              size="mini"
              @click="deleteRecord(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- View/Edit Record Dialog -->
      <el-dialog
        :trouble-describe="dialogTitle"
        :visible.sync="dialogVisible"
        wmaintain-idth="60%"
      >
        <el-form ref="currentRecord" :model="currentRecord" label-wmaintain-idth="120px">
          <el-form-item label="故障时间">
            <el-input v-model="currentRecord.maintainTime" />
          </el-form-item>
          <el-form-item label="维修人员">
            <el-input v-model="currentRecord.maintainer" />
          </el-form-item>
          <el-form-item label="维修状态">
            <el-select v-model="currentRecord.maintainState" style="width: 100%">
              <el-option label="已处理" value="已处理" />
              <el-option label="待处理" value="待处理" />
              <el-option label="处理中" value="处理中" />
            </el-select>
          </el-form-item>
          <el-form-item
            label="维修位置"
          >
            <el-input v-model="currentRecord.maintainPlace" />
          </el-form-item>
          <el-form-item label="维修信息" prop="message">
            <el-input
              v-model="currentRecord.troubleDescribe"
              type="textarea"
              autosize="{ minRows: 2, maxRows: 4 }"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
          <el-button type="primary" @click="saveChanges(currentRecord)">保存</el-button>
        </div>
      </el-dialog>
    </el-main>
  </div>
</template>

<script>import { InquiryMaintenance, DelMaintenance, UpdataMaintenance } from '@/api/maintenance'
export default {
  filters: {
    maintainStateFilter(maintainState) {
      const maintainStateMap = {
        已处理: 'success',
        待处理: 'danger',
        处理中: 'warning',
        待: 'danger'
      }
      return maintainStateMap[maintainState]
    }
  },
  data() {
    return {
      selected: 'repairRecords', // Initially selected option
      logForm: {
        name: '',
        maintainId: '',
        contact: '',
        department: '',
        deviceName: '',
        deviceLocation: '',
        issueDescription: '',
        issueTime: '',
        preOperation: '',
        issueType: []
        // Add more fields as needed
      },
      repairRecords: [ // Sample repair records, replace with actual data
        {
          receiveTime: '2024/3/14 19:08',
          startTime: '2024/3/14 19:15',
          endTime: '2024/3/14 19:38',
          repairPerson: '小明',
          message: '已经修复好215机房所有电脑visio安装问题'
        }
        // Add more records as needed
      ],
      listLoading: false,
      dialogVisible: false,
      dialogTitle: '',
      list: [
        // {
        //   maintainId: 1,
        //   maintainTime: '2024-03-14 19:08',
        //   maintainer: '石伟凡',
        //   troubleDescribe: 'Visio软件不可用',
        //   maintainState: '待处理'
        // },
        // {
        //   maintainId: 2,
        //   maintainTime: '2024-03-15 10:30',
        //   maintainer: 'YSY',
        //   troubleDescribe: '网络连接问题',
        //   maintainState: '处理中'
        // },
        // {
        //   maintainId: 3,
        //   maintainTime: '2024-03-16 14:45',
        //   maintainer: 'WWZ',
        //   troubleDescribe: '电源问题',
        //   maintainState: '已处理'
        // },
        // {
        //   maintainId: 4,
        //   maintainTime: '2024-03-17 08:20',
        //   maintainer: '石伟凡',
        //   troubleDescribe: '屏幕显示异常',
        //   maintainState: '待处理'
        // },
        // {
        //   maintainId: 5,
        //   maintainTime: '2024-03-18 13:55',
        //   maintainer: 'ZY',
        //   troubleDescribe: '键盘损坏',
        //   maintainState: '处理中'
        // },
        // {
        //   maintainId: 6,
        //   maintainTime: '2024-03-19 11:10',
        //   maintainer: 'YKF',
        //   troubleDescribe: '电脑死机',
        //   maintainState: '处理中'
        // },
        // {
        //   maintainId: 7,
        //   maintainTime: '2024-03-20 16:30',
        //   maintainer: '石伟凡',
        //   troubleDescribe: '无法启动',
        //   maintainState: '已处理'
        // },
        // {
        //   maintainId: 8,
        //   maintainTime: '2024-03-21 09:45',
        //   maintainer: '石伟凡',
        //   troubleDescribe: 'USB接口失灵',
        //   maintainState: '待处理'
        // }
      ],
      currentRecord: {
        maintainId: '',
        maintainPlace: '',
        maintainer: '',
        maintainTime: '',
        maintainDevice: '',
        troubleDescribe: '',
        maintainState: ''
      }
    }
  },
  created() {
    InquiryMaintenance()
      .then(res => {
        this.list = res.data.maintainList
      })
      .catch(error => {
        console.error(error)
      })
  },
  methods: {
    formatDate(dateString) {
      const date = new Date(dateString)
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      const hours = date.getHours().toString().padStart(2, '0')
      const minutes = date.getMinutes().toString().padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}`
    },
    handleSelect(selected) {
      this.selected = selected
    },
    viewRecord(record) {
      this.dialogTitle = '查看维修记录'
      // Copy the data from the record to currentRecord
      this.currentRecord = { ...record }
      this.dialogVisible = true
    },
    editRecord(record) {
      this.dialogTitle = '编辑维修记录'
      this.currentRecord = { ...record } // Make a copy to avoid modifying the original record directly
      this.dialogVisible = true
    },
    saveChanges(currentRecord) {
      console.log(currentRecord)
      UpdataMaintenance({
        'maintainId': currentRecord.maintainId,
        'maintainDevice': currentRecord.maintainDevice,
        'maintainPlace': currentRecord.maintainPlace,
        'maintainer': currentRecord.maintainer,
        'maintainTime': currentRecord.maintainTime,
        'troubleDescribe': currentRecord.troubleDescribe,
        'maintainState': currentRecord.maintainState
      })
        .then(res => {
          InquiryMaintenance()
            .then(res => {
              this.list = res.data.maintainList
            })
            .catch(error => {
              console.error(error)
            })
          this.$message({
            type: 'success',
            message: res.message
          })
        })
        .catch(error => {
          console.error(error)
        })
      this.dialogVisible = false
    },
    deleteRecord(record) {
      // Optionally, show a confirmation dialog
      // console.log(record)
      this.$confirm('确认删除该记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // Find the index of the record in the list
        const index = this.list.indexOf(record)
        if (index !== -1) {
          // Remove the record from the list
          DelMaintenance(record.maintainId).then(res => {
            this.list = this.list.concat(res.data.maintainList)
            this.$message({
              type: 'success',
              message: res.message
            })
          })
            .catch(error => {
              console.error(error)
            })
          this.list.splice(index, 1)
          this.$message({
            type: 'success',
            message: '记录删除成功'
          })
        } else {
          // Record not found in the list
          this.$message.error('找不到要删除的记录')
        }
      }).catch(() => {
        // User canceled deletion
        this.$message.info('已取消删除操作')
      })
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  height: 100vh; /* Full height */
}

.el-asmaintainIde {
  background-color: #f0f2f5;
}

.el-main {
  padding: 20px;
}

.el-divmaintainIder {
  margin: 20px 0;
}
</style>
